//
//  STUserApi.swift
//  Story
//
//  Created by 汤泽川 on 2019/4/2.
//  Copyright © 2019 汤泽川. All rights reserved.
//

import Foundation

class STUserAPI {
    static public func register(username : String, password : String, email : String, verifyCode : String, result : @escaping (_ error : STError?) -> Void) {
        defineUser(username: username, password: password, email: email, verifyCode: verifyCode, requestPath: "/users/register", result: result)
    }
    
    static public func resetPassword(username : String, password : String, email : String, verifyCode : String, result : @escaping (_ error : STError?) -> Void) {
        defineUser(username: username, password: password, email: email, verifyCode: verifyCode, requestPath: "/users/reset_password", result: result)
    }
    
    fileprivate static func defineUser(username : String, password : String, email : String, verifyCode : String, requestPath : String, result : @escaping (_ error : STError?) -> Void) {
        let request = STRequest<ResponseModel<String> >()
        request.requestPath = requestPath
        request.requestParam = ["username" : username, "password" : password, "email" : email, "verifyCode" : verifyCode]
        request.sendRequest { (state) in
            switch state {
            case .success(let data):
                if data.status != 0 {
                    result(STError(code: data.status, reason: data.message, errorType: .customError))
                } else {
                    result(nil)
                }
            case .fail(let error):
                result(error)
            }
        }
    }
    
    static public func login(username : String, password : String, result : @escaping (_ userInfo : ResponseModel<LoginResponseModel>?, _ error : STError?) -> Void) {
        let request = STRequest<ResponseModel<LoginResponseModel> >()
        request.requestPath = "/users/login"
        request.requestParam = ["username" : username, "password" : password]
        request.sendRequest { (state) in
            switch state {
            case .success(let data):
                if data.isSuccess {
                    result(data, nil)
                } else {
                    result(nil, STError(code: data.status, reason: data.message, errorType: .customError))
                }
            case .fail(let error):
                result(nil, error)
            }
        }
    }
    
    static public func logout(result : @escaping (_ userInfo : ResponseModel<String>?, _ error : STError?) -> Void) {
        let request = STRequest<ResponseModel<String> >()
        request.requestPath = "/users/logout"
        request.sendRequest { (state) in
            switch state {
            case .success(let data):
                if data.isSuccess {
                    result(data, nil)
                } else {
                    result(nil, STError(code: data.status, reason: data.message, errorType: .customError))
                }
            case .fail(let error):
                result(nil, error)
            }
        }
    }
    
    static public func quaryUserData(result : @escaping (_ userInfo : ResponseModel<STUserDataModel>?, _ error : STError?) -> Void) {
        let request = STRequest<ResponseModel<STUserDataModel> >()
        request.requestPath = "/users/quary_user_data"
        request.sendRequest { (state) in
            switch state {
            case .success(let data):
                if data.isSuccess {
                    result(data, nil)
                } else {
                    result(nil, STError(code: data.status, reason: data.message, errorType: .customError))
                }
            case .fail(let error):
                result(nil, error)
            }
        }
    }
    
    static public func quaryFollowStatus(targetId : Int, result : @escaping (_ userInfo : ResponseModel<Dictionary<String, Int> >?, _ error : STError?) -> Void) {
        let request = STRequest<ResponseModel<Dictionary<String, Int> > >()
        request.requestPath = "/users/follow_status"
        request.requestParam = ["targetId" : targetId]
        request.sendRequest { (state) in
            switch state {
            case .success(let data):
                if data.isSuccess {
                    result(data, nil)
                } else {
                    result(nil, STError(code: data.status, reason: data.message, errorType: .customError))
                }
            case .fail(let error):
                result(nil, error)
            }
        }
    }
    
    static public func changeFollowStatus(targetId : Int, status : Int, result : @escaping (_ userInfo : ResponseModel<String>?, _ error : STError?) -> Void) {
        let request = STRequest<ResponseModel<String> >()
        request.requestParam = ["targetId" : targetId, "status" : status]
        request.requestPath = "/users/follow"
        request.sendRequest { (state) in
            switch state {
            case .success(let data):
                if data.isSuccess {
                    result(data, nil)
                } else {
                    result(nil, STError(code: data.status, reason: data.message, errorType: .customError))
                }
            case .fail(let error):
                result(nil, error)
            }
        }
    }
    
    static public func quaryFollowList(page : Int, count : Int, result : @escaping (_ userInfo : ResponseModel<[STUserInfo]>?, _ error : STError?) -> Void) {
        let request = STRequest<ResponseModel<[STUserInfo]> >()
        request.requestParam = ["page" : page, "count" : count]
        request.requestPath = "/users/followlist"
        request.sendRequest { (state) in
            switch state {
            case .success(let data):
                if data.isSuccess {
                    result(data, nil)
                } else {
                    result(nil, STError(code: data.status, reason: data.message, errorType: .customError))
                }
            case .fail(let error):
                result(nil, error)
            }
        }
    }
    
    static public func changeProfile(avatarUrl : String?, introduce : String?, result : @escaping (_ userInfo : ResponseModel<STUserInfo>?, _ error : STError?) -> Void) {
        let request = STRequest<ResponseModel<STUserInfo> >()
        var param = Dictionary<String, Any>()
        if avatarUrl != nil {
            param["avatarUrl"] = avatarUrl!
        }
        if introduce != nil {
            param["introduce"] = introduce!
        }
        
        request.requestParam = param
        request.requestPath = "/users/change_profile"
        request.sendRequest { (state) in
            switch state {
            case .success(let data):
                if data.isSuccess {
                    result(data, nil)
                } else {
                    result(nil, STError(code: data.status, reason: data.message, errorType: .customError))
                }
            case .fail(let error):
                result(nil, error)
            }
        }
    }
}
